In PHP, the `header` function is used to send raw HTTP headers to the client. HTTP headers are crucial for controlling the flow of information between the client (usually a web browser) and the server, and they provide metadata about the HTTP request or response. The `header` function enables you to modify the standard set of HTTP headers or to add new ones.
The basic syntax of the `header` function is as follows:
```
header(string $header, bool $replace = true, int $response_code = 0)
```
- `$header` (string) — This parameter contains the header string to be sent.
- `$replace` (boolean) — Optional. If set to `true` (default), the function will replace a previous header of the same name. If set to `false`, it will append the new header (useful for specifying multiple `Set-Cookie` headers, for example).
- `$response_code` (integer) — Optional. Forces the HTTP response code to the specified value.
1. Redirecting the User:
Redirecting a user to another page is one of the most common uses of the `header` function. Here’s an example: \`\`\`php header(“Location: http://www.example.com/”); exit(); \`\`\`- This sends a 302 Found status code by default, which instructs the browser to navigate to the specified URL.
1. Setting Content-Type:
You can set the `Content-Type` of the response to ensure that the browser processes it correctly. For example, to specify that the content is JSON: \`\`\`php header(“Content-Type: application/json”); \`\`\`1. Forcing File Download:
To prompt the user to download a file instead of displaying it in the browser, you can send the `Content-Disposition` header: \`\`\`php header(“Content-Disposition: attachment; filename=“example.txt”“); \`\`\`1. Controlling Cache:
You can manage caching behavior to ensure that the client doesn’t cache the response inappropriately: \`\`\`php header(“Cache-Control: no-cache, no-store, must-revalidate”); // HTTP 1.1. header(“Pragma: no-cache”); // HTTP 1.0. header(“Expires: 0”); // Proxies. \`\`\`
- Headers Must Be Sent Before Output:
Headers must be sent before any actual output is sent to the browser. This means you should call `header` functions before any HTML or echo statements. If you fail to do so, PHP will throw a “headers already sent” warning.- Multiple Headers:
If you need to send multiple headers, you can call the `header` function multiple times: \`\`\`php header(“Content-Type: application/json”); header(“X-Custom-Header: MyValue”); \`\`\`
1. Redirecting with 301 Moved Permanently:
\`\`\`php header(“Location: http://www.example.com/”, true, 301); exit(); \`\`\`1. Sending Custom Headers:
\`\`\`php header(“X-Generated-By: PHP/” . phpversion()); \`\`\`
- PHP Official Documentation:
- [PHP: header – Manual](https://www.php.net/manual/en/function.header.php)
- W3C Schools:
- [PHP header() Function](https://www.w3schools.com/php/func_http_header.asp)
- MDN Web Docs:
- [HTTP headers – HTTP | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers)
In conclusion, the `header` function in PHP is an essential tool for managing HTTP headers, allowing for tasks such as redirection, content type specification, and caching control. Its flexibility and utility make it a fundamental part of server-side scripting in PHP. By understanding and utilizing this function wisely, developers can ensure smoother and more controlled interactions between the server and clients.